﻿#pragma execution_character_set("utf-8")
#include "communicatesavedelegate.h"

#include <QDebug>

CommunicateSaveDelegate::CommunicateSaveDelegate()
{
    hmiCommunicate = new HmiCommunicate();
}

CommunicateSaveDelegate::~CommunicateSaveDelegate()
{
    qDebug()<<"CommunicateSaveDelegate::run() end";
    delete hmiCommunicate;
}

int CommunicateSaveDelegate::setUserInfo(UserInfo userInfoIn)
{
    tmpUserInfo=userInfoIn;
    return 1;
}

void CommunicateSaveDelegate::updateTmpInfo(QString serverIP)
{
    //if(hmiCommunicate->globalData->tryLock(TRYLOCKTIME))
//    {
////        tmpUserInfo.account = hmiCommunicate->globalData->userInfo.account;
////        tmpUserInfo.ip = hmiCommunicate->globalData->userInfo.ip;
////        tmpUserInfo.password = hmiCommunicate->globalData->userInfo.password;
////        tmpUserInfo.operationSystem = hmiCommunicate->globalData->userInfo.operationSystem;



//        //hmiCommunicate->globalData->unlock();

////        qDebug() << "CommunicateSaveDelegate::updateTmpInfo tmpUserInfo.ip =" << QString::fromStdString(hmiCommunicate->globalData->userInfo.ip);
//    }
    usingMutex.lock();
    tmpUserInfo=hmiCommunicate->globalData->getUserInfo();
    tmpServerIp = serverIP;
    tmpServerIpArray = serverIP.toLatin1();
    usingMutex.unlock();
}

int CommunicateSaveDelegate::pauseRunSlot()
{
    if(""==tmpServerIp)
    {
        return -1;
    }
    //qDebug()<<"****stop!";

    int returnResult=-1;
    usingMutex.lock();
    if(soap_call_programCease(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult) == SOAP_OK)
    {
        qDebug()<<"CommunicateSaveDelegate:: pauseRunSlot soap_call_programCease";
//        if(returnResult == 1)
//        {
//        }
//        else
//        {
//        }
    }
    else
    {
        //再次发送
        soap_call_programCease(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult);
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
    return returnResult;
}

int CommunicateSaveDelegate::stopRun()
{
    qDebug()<<"CommunicateSaveDelegate::stopRun soap_call_programReset";
    if(""==tmpServerIp)
    {
        return -1;
    }
    int returnResult=-1;

    usingMutex.lock();
    if(soap_call_programReset(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult) == SOAP_OK)
    {
//        if(returnResult == 1)
//        {
//        }
//        else
//        {
//        }
    }
    else
    {
        //再次发送
        soap_call_programReset(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult);
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
    return returnResult;
}

int CommunicateSaveDelegate::stopWatchDog()
{
    if(""==tmpServerIp)
    {
        return -1;
    }
    int returnResult=-1;
    usingMutex.lock();
    if(soap_call_manual_stop(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult) == SOAP_OK)
    {
        qDebug()<<"CommunicateNormalDelegate::soap_call_manual_stop stopWatchDog= ok";
    }
    else
    {

    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
    return returnResult;
}


int CommunicateSaveDelegate::jogRunDogSlot()
{
    usingMutex.lock();
    int returnResult;

    if(soap_call_jogRunDog(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult) == SOAP_OK)
    {
//        if(returnResult == 1)
//        {
//        }
//        else
//        {
//        }
    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock(); return returnResult;
}


void CommunicateSaveDelegate::setManualServonSlot(int onOff)
{
    if(""==tmpServerIp)
    {
        return ;
    }
    //qDebug()<<"servon:"<<onOff;
    int returnResult;

    usingMutex.lock();
    if(soap_call_setManualServon(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,onOff,returnResult) == SOAP_OK)
    {

    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
}

void CommunicateSaveDelegate::setDragControl_slot(bool onOff)
{
    if(""==tmpServerIp)
    {
        return ;
    }
    //qDebug()<<"servon:"<<onOff;
    int returnResult;

    usingMutex.lock();
    if(soap_call_setDragControl(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",
                                tmpUserInfo,onOff,returnResult) == SOAP_OK)
    {

    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
}

int CommunicateSaveDelegate::runSlot()
{
    usingMutex.lock();
    int returnResult=-1;

    if(soap_call_programRun(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult) == SOAP_OK)
    {
        qDebug()<<"CommunicateSaveDelegate::runSlot():"<<returnResult;
    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
    return returnResult;
}


int CommunicateSaveDelegate::returnZeroSlot(int axisNum)
{
    usingMutex.lock();
    int returnResult=-1;
    if(soap_call_returnZeroPoint(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,axisNum,returnResult) == SOAP_OK)
    {
//        if(returnResult == 1)
//        {
//        }
    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
     return returnResult;
}

int CommunicateSaveDelegate::returnZeroSlot()
{
    usingMutex.lock();
    qDebug()<<"CommunicateSaveDelegate returnZeroSlot()";
    int returnResult=-1;
    if(soap_call_returnAllZeroPoint(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,returnResult) == SOAP_OK)
    {
//        if(returnResult == 1)
//        {
//        }
    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
    return returnResult;
}

void CommunicateSaveDelegate::jogRunSlot(int axis, int direct)
{
    qDebug()<<"CommunicateSaveDelegate::jogRunDogSlot axis"<<axis<<"direct"<<direct;
    usingMutex.lock();
    int returnResult;

    if(soap_call_jogRun(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,axis,direct,returnResult) == SOAP_OK)
    {
//        if(returnResult == 1)
//        {
//        }
//        else
//        {
//        }
    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
}



void CommunicateSaveDelegate::moveToPointSlot(MoveParameter movePoint)
{
    usingMutex.lock();
    //qDebug()<<"******moveToPointSlot";
    qDebug()<<"******moveToPointSlot:mv type "<<movePoint.moveType;
//    for(int i = 0;i<movePoint.targetPoint.positionValue.size();i++)
//    {
//        qDebug()<<"******moveToPointSlot:"<<movePoint.targetPoint.positionValue[i];
//    }
    int returnResult;
    if(soap_call_moveToPoint(&hmiCommunicate->client_soap,(const char*)tmpServerIpArray.data(),"",tmpUserInfo,movePoint,returnResult) == SOAP_OK)
    {
    }
    else
    {
        //qDebug("soap error:%d,%s,%s", hmiCommunicate->client_soap.error, *soap_faultcode(&hmiCommunicate->client_soap), *soap_faultstring(&hmiCommunicate->client_soap) );
    }
    soap_destroy(&hmiCommunicate->client_soap);
    soap_end(&hmiCommunicate->client_soap);
    soap_done(&hmiCommunicate->client_soap);
    usingMutex.unlock();
}

